package com.welab.task3.dto.response;

import com.welab.task3.enumeration.StatusEnum;
import com.welab.task3.exceptions.BaseException;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author dammein.yuan
 * @version v1.0
 * @Title: com.welab.task3.dto.response.Result
 * @Description: 返回结果封装
 * @Copyright: © 2020 ***
 * @Company: ***有限公司
 * @date 2020-3-9
 */
@Data
@ApiModel("返回结果实体")
public class Result<T> {
    @ApiModelProperty("数据")
    private T data;
    @ApiModelProperty(value = "状态码",example = "0")
    private Integer code;
    @ApiModelProperty("信息")
    private String msg;

    Result() {
    }

    Result(T t, StatusEnum statusEnum) {

        this.data = t;
        this.code = statusEnum.getCode();
        this.msg = statusEnum.getMsg();

    }

    Result(BaseException exception){
        this.code=exception.getCode();
        this.msg=exception.getMsg();

    }

    public static<T> Result<T> success(T data){
        return new Result(data,StatusEnum.SUCCESS);
    }
    public static<T> Result<T> success(T data,StatusEnum statusEnum){
        return new Result(data,statusEnum);
    }

    public static Result fail(BaseException exception){
        return new Result(exception);
    }
    public static<T> Result<T> fail(T data){
        return new Result(data,StatusEnum.FAIL);
    }
}
